﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using LibNoise.Xna;
using LibNoise.Xna.Generator;
using LibNoise.Xna.Operator;

namespace GameDev
{
    class Map
    {
        
        int width;
        int height;
        float res;
        VertexPositionColor[] vertices;
        int[] indices;
        float[,] heightData;
        Perlin  perlinNoise;
        
        public Map(int w, int h)
        {
            width = w;
            height = h;

        }

        public void SetUpVertices()
        {
            vertices = new VertexPositionColor[width * height];
            res = 1f;
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    vertices[x + y * width].Position = new Vector3(x * res, heightData[x, y] * res, -y * res);
                    vertices[x + y * width].Color = new Color(heightData[x, y]/20+0.5f,heightData[x, y]/20+0.5f,heightData[x, y]/20+0.5f);
                }
            }

        }


        public void SetUpIndices()
        {
            indices = new int[(width - 1) * (height - 1) * 6];
            int count = 0;

            for (int y = 0; y < height - 1; y++)
            {
                for (int x = 0; x < width - 1; x++)
                {
                    int lowerLeft = x + y * width;
                    int lowerRight = (x + 1) + y * width;
                    int topLeft = x + (y + 1) * width;
                    int topRight = (x + 1) + (y + 1) * width;

                    indices[count++] = topLeft;
                    indices[count++] = lowerRight;
                    indices[count++] = lowerLeft;

                    indices[count++] = topLeft;
                    indices[count++] = topRight;
                    indices[count++] = lowerRight;
                }
            }
        }


        public void LoadHeightData()
        {
            perlinNoise = new LibNoise.Xna.Generator.Perlin(0.09, 1, 1, 10, 13, LibNoise.Xna.QualityMode.High);
            
            heightData = new float[width, height];
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    
                    //heightData[x, y] = SimplexNoiseGenerator.Generate ((float)(PerlinNoiseGenerator.Noise(x,y)))*10;
                    heightData[x, y] = (float)perlinNoise.GetValue(x, y,x);
                    Console.WriteLine(heightData[x, y].ToString());
                }
            }
        }

        public void update()
        {

        }

        public VertexPositionColor[] getVertices()
        {
            return vertices;
        }

        public int[] getIndices()
        {
            return indices;
        }

        public Vector2 getDimension()
        {
            return new Vector2(width, height);
        }








    }
}
